﻿dojo.provide("dojot.album.MyGallery");
dojo.experimental("dojot.album.MyGallery");
dojo.require("dojot.album.Gallery");
dojo.require("dojot.comment.Comment");
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojot.util.EditInPlaceField");
dojo.require("dojot.group2.security");
dojo.requireLocalization("dojot", "common");
dojo.requireLocalization("dojot", "album");

dojo.declare("dojot.album.MyGallery",[dijit._Widget, dijit._Templated,dojot.album.Gallery],{
	//模板url
	templatePath: dojo.moduleUrl("dojot.album", "resources/MyGallery.html"), 
	widgetsInTemplate: true,
	//当前图片的原始大小的url
	currentOriPicUrl:"",
	//相册id
	albumId:"",
	
	//要看的图片索引
	showIndex:"",
	
	//当前的图片id
	picId:"",
	
	//相册一些操作的url,比如删除相册图片，设置图片为封面
	albumPicEditUrl:"",
	
	//增加相册预览数量的url
	albumPicBrowseCountUrl:"",
	
	//获取图片列表url
	albumPicPreviewListUrl:"",
	
	//图片编辑器模块
	picDescEdit:null,
	
	//定义相册额外信息的模板
	picExtraInfoTemplateUrl:dojo.moduleUrl("dojot.album","resources/PicExtraInfo.html"),
	
	picExtraInfoTpl: null,
	
	
	postCreate: function(){
		this.inherited(arguments);
		//get desc node area
		
		//create album
		this.gallery = new dojot.album.Gallery({},this.galleryNode);
		
		//init album store
		var imageItemStore = new dojo.data.ItemFileWriteStore({url:this.albumPicPreviewListUrl});
			
		var itemRequest = {
			showIndex:this.showIndex
		};
		var itemNameMap = {
			 imageThumbAttr: "small",
			 imageLargeAttr: "large",
			 titleAttr:"desc",
			 linkAttr:"orig"
		};
		//set album data
		this.gallery.setDataStore(imageItemStore, itemRequest,itemNameMap);		
		
		this.picIdBox = dojo.byId("picIdBox");	
		
		var that = this;
		//subscribe pic show event,for get oriPicUrl and show comment
		dojo.subscribe(this.gallery.slideShow.getShowTopicName(), function(packet){
			that.currentOriPicUrl = packet.link;
			that.picId = packet.picId;
			//如果拥有评论组件则显示评论
			if(that.comment){
				that.comment.showAllComments(packet.picId);
			}
			that.addBrowseCountToServer(packet.picId);
			//设置picDescEdit的picId;
			if(that.getPicDescEditModule()){
				that.getPicDescEditModule().setPicId(packet.picId);
			}
			
			dojo.attr(that.picIdBox,"value",packet.picId);
		});
		//change delete link ownerid
		dojo.subscribe(this.gallery.slideShow.getShowTopicName(), function(packet){
			dojo.attr("delete_pic","ownerId",packet.ownerId);
			dojo.style("delete_pic","display","none");
			//判断权限
			if(dojot.security){
				dojot.security.isAccess("albumDetialToolbar");
				if(that.getPicDescEditModule()){
					if(!dojot.security.isHaveAccess(["owner","admin"],packet.ownerId)){
						that.getPicDescEditModule().setAllowChangeValue(false);
					}
				}
			}
		});
		
		this.picInfoExtraTpl = new dojox.dtl.Template(this.picExtraInfoTemplateUrl);
		dojo.subscribe("showPicExtraInfo",function(context){
			var picInfoExtraHtml = dojo.string.substitute(that.picInfoExtraTpl.render(context),{ 
				commentCount: that._messages["commentCount"],
				browserCount: that._messages["browserCount"],
				uploadTime: that._messages["uploadTime"]
			});
			dojo.byId("picExtraInfo").innerHTML = picInfoExtraHtml;
		});
		var messages1= dojo.i18n.getLocalization("dojot", "common",dojo.locale);
		var messages2= dojo.i18n.getLocalization("dojot", "album",dojo.locale);
		this._messages = dojo.mixin({},messages1,messages2);
		
		//订阅没有图片事件，如果没有图片了，需要把模块的picDescEdit模块内容清空，而且变为不可编辑状态
		//还要清空图片评论模块。
		dojo.subscribe("nopic",function(){
			//下面的操作有些多余，直接把gallery清空，然后提示没有图片直接完事
			that.domNode.innerHTML = "您的相册现在没有图片";
			
			//处理图片编辑模块
			if(that.getPicDescEditModule()){
				that.getPicDescEditModule().setAllowChangeValue(false);
				that.getPicDescEditModule().setValue("");	
			}
			//清空图片评论信息
			if(that.comment){
				that.comment.cleanComment(that.picId);
			}
			//清空图片描述额外信息
			dojo.byId("picExtraInfo").innerHTML = "";
			//清空评论输入框
			dojo.query(".reviewMore").forEach(function(node){
				node.innerHTML = "";
			});
		});
		
	},
	addBrowseCountToServer:function(picId){
		var that = this;
		dojo.xhrPost({
				url: that.albumPicBrowseCountUrl,
				content:{
					picId: picId
				},			
				handleAs: "json"
		});

	},
	setDataStore: function(dataStore, request,paramNames){
		this.gallery.setDataStore(dataStore, request, paramNames);
  	},
  	viewOriginalPic:function(){
		window.open (this.currentOriPicUrl);
	},
	//下面的三个方法其实可以统一成一个方法，然后可以根据传递的触发对象的类型，来选择不同的处理方式
	//否则判断photoSize有点显得冗余
	deleteImage:function(){
		if(this._getPhotoSize() <= 0){
			return false;
		}
		if(confirm(this._messages.confirmDelete)){
			var that = this;
			dojo.publish("albumPicDelete",[that]);
		}	
	},
	transferPic:function(){
		if(this._getPhotoSize() <= 0){
			return false;
		}
		var that = this;
		dojo.publish("albumTransfer",[that]);
	},
	setCover:function(){
		if(this._getPhotoSize() <= 0){
			return false;
		}
		if(confirm(this._messages.album2SetCoverSuggestInfo)){
			var that = this;
			dojo.xhrPost({
				url: that.albumPicEditUrl,
				content:{
					method: "cover",
					ids: that.picId
				},
				load:function(data){
					console.log(data);
				},
				handleAs: "json"
			});
		}		
	},
	setCommentModule:function(comment){
		this.comment = comment;
	},
	setPicDescEditModule: function(picDescEdit){
		this.gallery.slideShow.setPicDescEdit(picDescEdit);
	},
	getPicDescEditModule:function(){
		return this.gallery.slideShow.picDescEdit;
	},
	_getPhotoSize: function(){
		return this.gallery.slideShow.maxPhotos;
	}
});
